<template>
	<view class="content">
		<!-- 质量异常 -->
		<u-form :model="formData" ref="formData">
			<u-row gutter="">

				<u-col span="10">
					<!-- <u-form-item label="异常描述:" prop="exceptionName" label-width='150rpx'>
						<u-input v-model="formData.exceptionName" placeholder="" border />
					</u-form-item> -->
					<u-form-item label="异常描述:" prop="exceptionName" label-width='150rpx'>
						<u-radio-group v-model="formData.exceptionName">
							<u-radio v-for="(item, index) in checkboxList" :key="index" :name="item.exceptionName"
								:disabled="item.disabled">
								{{ item.exceptionName }}
							</u-radio>
						</u-radio-group>
					</u-form-item>
				</u-col>
				<u-col span="5">
					<u-form-item label="异常时间" prop="exceptionTime" :border-bottom="false" label-width='150rpx'>
						<u-input v-model="formData.exceptionTime" disabled disabledColor="#ffffff"
							placeholder=""></u-input>
					</u-form-item>
				</u-col>

			</u-row>
			<u-row gutter="0">
				<u-col span="4">
					<u-form-item :border-bottom="false" label="订单编号:" label-width='150rpx' prop="workorderName">
						<u-input v-model="formData.workorderCode" disabled placeholder="" />
					</u-form-item>
				</u-col>
				<u-col span="4">
					<u-form-item :border-bottom="false" label="发布工单:" label-width='150rpx' prop="releaseOrderCode">
						<u-input v-model="formData.releaseOrderCode" disabled placeholder="" />
					</u-form-item>
				</u-col>
				<u-col span="4">
					<u-form-item :border-bottom="false" label="产品牌号:" prop="productName" label-width='150rpx'>
						<u-input :disabled="true" v-model="formData.productName" placeholder="" />
					</u-form-item>
				</u-col>
				<u-col span="4">
					<u-form-item :border-bottom="false" label="生产批次:" prop="batchCode" label-width='150rpx'>
						<u-input :disabled="true" v-model="formData.batchCode" placeholder="" />
					</u-form-item>
				</u-col>
			</u-row>


		</u-form>
		<view style="margin: 10px 0;">
			<u-button type="primary" style="width: 300rpx;margin-bottom: 10rpx;" @click="submit">确定</u-button>
			<u-button type="primary" :plain="true" style="width: 300rpx;" @click="cancel">取消</u-button>
		</view>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	export default {
		props: {
			workorder: {
				default: Object
			}
		},
		data() {
			return {
				formData: {},
				// 异常描述
				checkboxList: [{
						exceptionName: '颜色不符',
						checked: false,
						disabled: false
					},
					{
						exceptionName: '性能不符',
						checked: false,
						disabled: false
					},
					{
						exceptionName: '外观不符',
						checked: false,
						disabled: false
					}, {
						exceptionName: '特殊特性不符',
						checked: false,
						disabled: false
					},
					{
						exceptionName: '包装标签不符',
						checked: false,
						disabled: false
					}
				],
			}
		},
		onLoad() {

		},
		async created() {
			console.log('abnormal', this.workorder)
			this.formData.batchCode = this.workorder.batchCode;
			this.formData.productName = this.workorder.productName;
			this.formData.releaseOrderCode = this.workorder.releaseOrderCode;
			this.formData.releaseOrderId = this.workorder.releaseOrderId;
			this.formData.workorderCode = this.workorder.workorderCode;
			// this.formData.releaseOrderId = this.workorder.releaseOrderId;
			// this.getBarCode()
			let result = await this.$u.api.getAutoCode('EXCEPTION_CODE');
			this.formData.exceptionCode = result;
			this.formData.exceptionTime = this.getCurrentDateTime()
			this.$forceUpdate()
		},
		methods: {
			// 获取当前日期
			getCurrentDateTime() {
				const now = new Date();

				const year = now.getFullYear();
				const month = String(now.getMonth() + 1).padStart(2, "0"); // 月份从 0 开始，需要加 1
				const day = String(now.getDate()).padStart(2, "0");

				const hours = String(now.getHours()).padStart(2, "0");
				const minutes = String(now.getMinutes()).padStart(2, "0");
				const seconds = String(now.getSeconds()).padStart(2, "0");

				return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
			},
			// 确认提交
			submit() {
				if (this.formData.exceptionName && this.formData.exceptionName != '') {
					this.$u.api.addException({
						releaseOrderId: this.formData.releaseOrderId,
						exceptionCode: this.formData.exceptionCode,
						exceptionName: this.formData.exceptionName,
						exceptionTime: this.formData.exceptionTime
					}).then(res => {
						if (res.code == 200) {

							// this.$refs.uToast.show({
							// 	title: '新增成功',
							// 	type: 'success',
							// })
							uni.showToast({
								title: '操作成功',
								//将值设置为 success 或者直接不用写icon这个参数
								icon: 'none',
								//显示持续时间为 2秒
								duration: 2000
							})
							setTimeout(() => {
								this.cancel(); // 触发父组件的关闭事件
								this.$emit("close-popup"); // 触发父组件的关闭事件
							})
						}else{
							uni.showToast({
								title: res.msg,
								//将值设置为 success 或者直接不用写icon这个参数
								icon: 'none',
								//显示持续时间为 2秒
								duration: 2000
							})
						}
					})
				} else {
					// this.$refs.uToast.show({
					// 	title: '请输入异常',
					// 	type: 'error',
					// })
					uni.showToast({
						title: '请输入异常',
						//将值设置为 success 或者直接不用写icon这个参数
						icon: 'none',
						//显示持续时间为 2秒
						duration: 2000
					})
				}

			},
			// 取消提交	
			cancel() {
				this.formData = {};
				this.$emit("close-popup"); // 触发父组件的关闭事件
			}
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		width: 100%;
		// height: 100%;
		display: flex;
		flex-direction: column;
		padding: 40rpx;
	}
</style>